package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.AttributeAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/attributes")
@Api(tags = "07.属性管理模块")
@Slf4j
public class AttributeController {

    @Autowired
    private IAttributeService attributeService;

    @PostMapping("/add-new")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)

    public JsonResult addNew(@Valid AttributeAddNewParam attributeAddNewParam) {
        log.debug("开始处理[添加属性模版]的请求，参数{}", attributeAddNewParam);
        attributeService.addNew(attributeAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "Long")
    })
    public JsonResult delete(
            @Range(min = 1, message = "根据ID删除属性失败，请提交合法的ID值！")
            @PathVariable Long id){
        attributeService.deleteById(id);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新属性")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "Long")
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新属性失败，请提交合法的ID值！")
            @PathVariable Long id,
            @Valid AttributeUpdateInfoParam attributeUpdateInfoParam){
        attributeService.updateInfoById(id, attributeUpdateInfoParam);

        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询属性详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询属性失败，请提交合法的ID值！")
            @PathVariable Long id){
        AttributeStandardVO standardById = attributeService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/list-by-template")
    @ApiOperation("查询属性列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "模板ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "请提交有效的相册ID值！") Long templateId,
            @Range(min = 1, message = "查询属性列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<AttributeListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = attributeService.listByTemplateId(templateId, pageNum, Integer.MAX_VALUE);
        }else{
            pageData = attributeService.listByTemplateId(templateId, pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
